package com.rfid.library.zhongshan;

import android.text.TextUtils;

import java.util.regex.Pattern;

/// <summary>
    /// 卷册年类
    /// </summary>
    public class VolumeOrYear
    {
        //#region 字段
        /// <summary>
        /// 分类号内容
        /// </summary>
         String cnStr = null;

        /// <summary>
        /// 分类号长度
        /// </summary>
        int len = 0;

        public VolumeOrYear(String i_voOrYear)
        {
            cnStr = i_voOrYear;
            len = i_voOrYear.length();
        }

        /// <summary>
        /// 按照卷册年规则来比较字符大小
        /// </summary>
        /// <param name="c1"> 字符1 </param>
        /// <param name="c2"> 字符2 </param>
        /// <returns> c1 &lt; c2 -1 <br> c1 == c2 0 <br> c1 &gt; c2 1 </returns>
         int compareChar(char c1, char c2)
        {
            String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@()（）-:：";

            return str.indexOf(c1) - str.indexOf(c2);
        }

        /// <summary>
        /// 按卷册年类比较
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        int compareJCH(String str1, String str2)
        {
            int index = 0, tmp;
            int strLength1 = str1.length(), strLength2 = str2.length();

            while ((strLength1 > index) && (strLength2 > index))
            {
                char c1 = str1.charAt(index);
                char c2 = str2.charAt(index);

                tmp = compareChar(c1, c2);

                if (tmp != 0)
                {
                    return tmp;
                }

                index++;
            }

            if ((strLength1 == index) && (strLength2 == index))
            {
                return 0;
            }

            if ((strLength1 == index) && (strLength2 > index))
            {
                return -1;
            }

            if ((strLength1 > index) && (strLength2 == index))
            {
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 比较大小
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public int CompareTo(Object o)
        {
//            BookIndex.Fun_Message("卷册号比较----------->\r\n");

            String s1 = this.cnStr;
            if (s1.substring(0, 1).equals("(") && s1.length() > 1)
            {
                s1 = s1.substring(1);
            }
            VolumeOrYear t_vy2 = (VolumeOrYear)o;
            String s2 = t_vy2.cnStr;
            if (s2.substring(0, 1).equals("(") && s2.length()>1)
            {
                s2 = s2.substring(1);
            }

            if (s1.equals(s2))
            {
                //相等
                return 0;
            }
            boolean s1B = TextUtils.isEmpty(s1);
            boolean s2B = TextUtils.isEmpty(s2);
            if (s1B && !s2B)
            {
                return -1;
            }
            else if(!s1B && s2B)
            {
                return 1;
            }
            else if (s1B && s2B)
            {
                return 0;
            }
            else
            {
                //都不是空 ，继续比较吧
            }

            //#region 策略 先分解
            StringProxy t_zy1Part1= new StringProxy("");
            StringProxy t_zy1Part2=new StringProxy("");
            StringProxy t_zy1Part3=new StringProxy("");
            StringProxy t_zy1Part4=new StringProxy("");
            StringProxy t_zy1Part5=new StringProxy("");
            StringProxy t_zy1Part6=new StringProxy("");
            StringProxy t_zy1Part7=new StringProxy("");
            StringProxy t_zy1Part8 = new StringProxy("");
//            BookIndex.Fun_Message(s1 + ":\r\n");
            Fun_Decompose("@()-:.", s1,  t_zy1Part1,  t_zy1Part2,  t_zy1Part3,  t_zy1Part4,  t_zy1Part5,  t_zy1Part6,  t_zy1Part7,  t_zy1Part8);

            StringProxy t_zy2Part1 = new StringProxy("");
            StringProxy t_zy2Part2 = new StringProxy("");
            StringProxy t_zy2Part3 = new StringProxy("");
            StringProxy t_zy2Part4 = new StringProxy("");
            StringProxy t_zy2Part5 = new StringProxy("");
            StringProxy t_zy2Part6 = new StringProxy("");
            StringProxy t_zy2Part7 = new StringProxy("");
            StringProxy t_zy2Part8 = new StringProxy("");
//            BookIndex.Fun_Message(s2 + ":\r\n");
            Fun_Decompose("@()-:.", s2,  t_zy2Part1,  t_zy2Part2,  t_zy2Part3,  t_zy2Part4,  t_zy2Part5,  t_zy2Part6,  t_zy2Part7,  t_zy2Part8);
//            #endregion

            //#region 再比较
            int t_result = 0;
            //判断第一部分
           // #region 判断第一部分
            t_result = Fun_CompareJCH(t_zy1Part1.str, t_zy2Part1.str);
            if (t_result != 0)
            {
                return t_result;
            }
            //#endregion

           // #region 比较第二部分
            t_result = Fun_CompareJCH(t_zy1Part2.str, t_zy2Part2.str);
            if (t_result != 0)
            {
                if (t_result == -1000) { return 0; }
                return t_result;
            }
            //#endregion

           // #region 比较第三部分
            t_result = Fun_CompareJCH(t_zy1Part3.str, t_zy2Part3.str);
            if (t_result != 0)
            {
                if (t_result == -1000) { return 0; }
                return t_result;
            }
            //#endregion

           // #region 比较第四部分
            t_result = Fun_CompareJCH(t_zy1Part4.str, t_zy2Part4.str);
            if (t_result != 0)
            {
                if (t_result == -1000) { return 0; }
                return t_result;
            }
            //#endregion

            //#region 比较第五部分
            t_result = Fun_CompareJCH(t_zy1Part5.str, t_zy2Part5.str);
            if (t_result != 0)
            {
                if (t_result == -1000) { return 0; }
                return t_result;
            }

            return 0;
        }

        /// <summary>
        /// 比较每个卷册号部分
        /// </summary>
        /// <param name="i_start"></param>
        /// <param name="i_end"></param>
        /// <returns></returns>
        public int Fun_CompareJCH(String i_Start, String i_End)
        {
            //#region 检查数据
            
            boolean t_StartB=TextUtils.isEmpty(i_Start);
            boolean t_EndB=TextUtils.isEmpty(i_End);
            if (t_StartB && t_EndB)
            {
                //都是空
                return -1000;
            }
            else if (!t_StartB && t_EndB)
            {
                return 1;
            }
            else if (t_StartB && !t_EndB)
            {
                return -1;
            }
            else
            {
                //去比较
            }

            if (i_Start.equals(i_End)) { return 0; }


            int t_Result = 0;
            String t_Start = i_Start;
            String t_End = i_End;
            int t_IStart = 0;
            int t_IEnd = 0;
            boolean t_StartIsN = false; ;
            boolean t_EndIsN=false;
            try
            {
                t_IStart = Convert.ToInt16(t_Start);
                t_StartIsN=true;
            }
            catch (Exception e){ }
            try
            {
                t_IEnd = Convert.ToInt16(t_End);
                t_EndIsN=true;
            }
            catch (Exception e){ }

            if (t_StartIsN && !t_EndIsN)
            {
                //数字  不是数字
                return -1;
            }
            if (!t_StartIsN && t_EndIsN)
            {
                //不是数字  数字
                return 1;
            }
            else if (t_StartIsN && t_EndIsN)
            {
                //都是数字

//                return t_IStart.CompareTo(t_IEnd);
                return t_IStart > t_IEnd ? 1 : (t_IStart ==t_IEnd ? 0 : -1);
            }
            else
            {
                //都不是数字
                //#region 都不是数字
                try
                {
                    t_Result = compareJCH(t_Start, t_End);
                    return t_Result;
                }
                catch (Exception e){ return 0; }
//                #endregion
            }
        }

        /// <summary>
        /// 分解卷册年度
        /// </summary>
        /// <param name="i_mark"></param>
        /// <param name="i_zy"></param>
        /// <param name="i_zyPART1"></param>
        /// <param name="i_zyPART2"></param>
        /// <param name="i_zyPART3"></param>
        /// <param name="i_zyPART4"></param>
        /// <param name="i_zyPART5"></param>
        /// <param name="i_zyPART6"></param>
        /// <param name="i_zyPART7"></param>
        private void Fun_Decompose(String i_mark, 
                String i_zy, 
                StringProxy i_zyPART1, 
                StringProxy i_zyPART2, 
                StringProxy i_zyPART3, 
                StringProxy i_zyPART4, 
                StringProxy i_zyPART5,
                StringProxy i_zyPART6,
                StringProxy i_zyPART7,
                StringProxy i_zyPART8)
        {
            String t_vy = i_zy;//.Replace(")", "")
            if (t_vy.contains(")"))
            {
                int t_index = t_vy.indexOf(")");
                if (t_index < t_vy.length() - 1)
                {
                    String t_BeHindOnex = t_vy.charAt(t_index + 1) + "";
                    boolean t_IsN = Pattern.matches(".*[0-9].*",t_BeHindOnex);
                    if (t_IsN)
                    {
                        t_vy = t_vy.replace(")", "@");
                    }
                    else
                    {
                        t_vy = t_vy.replace(")", "");
                    }
                }
                else
                {
                    t_vy=t_vy.replace(")", "");
                }
            }
            int t_PartLenth = 0;
            int t_PreIndex = 0;

            //首字母是符号 true  首字母不是符号false
            boolean t_Type = i_mark.contains(t_vy.charAt(0) + "");

            for (int i = 0; i < t_vy.length(); i++)
            {
                if (i_mark.contains(t_vy.charAt(i) + ""))
                {
                    int t_markIndex = i;
                    String t_str = "";

                    if (t_Type)
                    {
                        //首字母是符号
                        if (i == 0)
                        {
                            t_str = t_vy.charAt(i) + "";
                        }
                        else
                        {
                            if (t_PartLenth == 0)
                            {
                                t_str = t_vy.substring(0, i);
                            }
                            else
                            {
                                t_str = t_vy.substring(t_PreIndex + 1, t_markIndex - t_PreIndex - 1);
                            }
                        }
                    }
                    else
                    {
                        //首字母不是符号
                        if (t_PreIndex == 0)
                        {
                            t_str = t_vy.substring(0, i);
                        }
                        else
                        {
                            t_str = t_vy.substring(t_PreIndex + 1, t_markIndex - t_PreIndex - 1);
                        }
                        
                    }
                    if (TextUtils.isEmpty(t_str))
                    {
                        t_str = t_vy.charAt(i) + "";
                    }

                    switch (t_PartLenth)
                    {
                        case 0:
                            i_zyPART1.str = t_str;
                            if (!t_Type)
                            {
                                i_zyPART2.str = t_vy.charAt(i) + "";
                                t_PartLenth++;
                            }
                            break;
                        case 1:
                            if (t_Type)
                            {
                                i_zyPART2.str = t_str;
                                i_zyPART3.str = t_vy.charAt(i) + "";
                            }
                            else
                            {
                                i_zyPART3.str = t_str;
                                i_zyPART4.str= t_vy.charAt(i) + "";
                            }
                            t_PartLenth++;
                            break;
                        case 2:
                            i_zyPART3.str = t_str;
                            i_zyPART4.str = t_vy.charAt(i) + "";
                            t_PartLenth++;
                            break;
                        case 3:
                            if (t_Type)
                            {
                                i_zyPART4.str = t_str;
                                i_zyPART5.str = t_vy.charAt(i) + "";
                            }
                            else
                            {
                                i_zyPART5.str = t_str;
                                i_zyPART6.str = t_vy.charAt(i) + "";
                            }
                            t_PartLenth++;
                            break;
                        case 5:
                            if (t_Type)
                            {
                                i_zyPART6.str = t_str;
                                i_zyPART7.str = t_vy.charAt(i) + "";
                            }
                            else
                            {
                                i_zyPART7.str = t_str;
                                i_zyPART8.str = t_vy.charAt(i) + "";
                            }
                            t_PartLenth++;
                            break;
                        default:
                            break;
                    }

                    t_PreIndex = i;
                    t_PartLenth++;
                }

            }

            if (t_PreIndex < t_vy.length())
            {
                //剩余字符
                String t_str = "";
                if (t_PreIndex == 0 && t_PartLenth == 0)
                {
                    t_str = t_vy;
                }
                else
                {
                    t_str = t_vy.substring(t_PreIndex + 1);
                }
                
                switch (t_PartLenth)
                {
                    case 0:
                        i_zyPART1.str = t_str;
                        break;
                    case 1:
                        i_zyPART2.str = t_str;
                        break;
                    case 2:
                        i_zyPART3.str= t_str;
                        break;
                    case 3:
                        i_zyPART4.str= t_str;
                        break;
                    case 4:
                        i_zyPART5.str = t_str;
                        break;
                    case 5:
                        i_zyPART6.str= t_str;
                        break;
                    case 6:
                        i_zyPART7.str = t_str;
                        break;
                    case 7:
                        i_zyPART8.str = t_str;
                        break;
                    default:
                        break;
                }
            }


//            BookIndex.Fun_Message("分解：一>> " + i_zyPART1.PadRight(6, ' ')
//                + "二>> " + i_zyPART2.PadRight(5, ' ')
//                + "三>> " + i_zyPART3.PadRight(5, ' ')
//                + "四>> " + i_zyPART4.PadRight(5, ' ')
//                + "五>> " + i_zyPART5.PadRight(5, ' ')
//                + "六>> " + i_zyPART6.PadRight(5, ' ')
//                + "七>> " + i_zyPART7.PadRight(5, ' ')
//                + "八>> " + i_zyPART8 + "\r\n");
        }
    }